/**
 * @file factory_mode.h
 *
 */
#ifndef FACTORY_MODE_H
#define FACTORY_MODE_H

#ifdef __cplusplus
extern "C" {
#endif

/*********************
 *      INCLUDES
 *********************/

#ifdef LV_CONF_INCLUDE_SIMPLE
#include "lvgl.h"
#include "lv_watch_conf.h"
#else
#include "../../../lvgl/lvgl.h"
#include "../../../lv_watch_conf.h"
#endif
#include "../../../lv_drivers/indev/crane_watch_keypad.h"
#include "factory_mode_config.h"

#if USE_LV_WATCH_FACTORY_MODE != 0

/*********************
 *      DEFINES
 *********************/
#define AUTOTEST_ITEM_NUM NV_FACTORYMODE_TEST_NUM
#define AGINGTEST_ITEM_NUM NV_FACTORYMODE_AGINGTEST_NUM
#define HWTEST_LIST_CNT    9

/**********************
 *      TYPEDEFS
 **********************/
enum
{
    TEST_NOTDONE = 0,
    TEST_FAILED,
    TEST_SUCCESS,
};
typedef uint8_t factorymode_test_res_t;
enum
{
    TEST_MANUAL = 0,
    TEST_AUTO,
    TEST_AGING,
};
typedef uint8_t factorymode_test_mode_t;

typedef struct {
    lv_watch_obj_ext_t lv_watch_obj;
    /*New data for this type */
    lv_style_t red_st;
    lv_style_t green_st;
    lv_style_t blue_st;
    lv_style_t white_st;
    lv_style_t gray_st;
    lv_style_t orange_st;
    lv_style_t purpul_st;
    lv_style_t teal_st;
    lv_style_t cyan_st;
    lv_style_t list_btn_st;
    lv_obj_t *suc_btn;
    factorymode_test_res_t test_res[AUTOTEST_ITEM_NUM];
    factorymode_test_res_t agingtest_res[AGINGTEST_ITEM_NUM];
    factorymode_test_mode_t testmode;
    uint8_t lcdarea_click_num;
    uint8_t speaker_volume;
    uint8_t module;
    bool istest[AUTOTEST_ITEM_NUM];
    lv_obj_t *label_key_state;  //valid when 'module' is AUTOTEST_PRESS_KEY
    uint8_t * buffer;
    uint32_t buffer_size;
    lv_task_t * record_task;
    MCI_EVNET_T event;
    MCI_INFO_T info_type;
    int32_t value;
    TIMER_ID timer;
    lv_img_dsc_t img_camera;
    lv_task_t * preview_task;
    uint8_t aging_lcd_brightness;
} lv_factory_obj_ext_t;

typedef struct {
    lv_watch_obj_ext_t lv_watch_obj;
    /*New data for this type */
    lv_obj_t *par_obj;
} lv_autotest_obj_ext_t;

typedef struct {
    lv_watch_obj_ext_t lv_watch_obj;
    /*New data for this type */
    lv_obj_t *btn[AUTOTEST_ITEM_NUM];
} lv_testreport_obj_ext_t;

typedef struct {
    lv_watch_obj_ext_t lv_watch_obj;
    /*New data for this type */
    lv_obj_t *btn[AGINGTEST_ITEM_NUM];
} lv_aging_obj_ext_t;

typedef struct
{
    lv_watch_obj_ext_t lv_watch_obj;
    /*New data for this type */
    bool select_all;
    bool test_started;
    bool hwtest_sel[HWTEST_LIST_CNT];
    lv_obj_t * list_btn[HWTEST_LIST_CNT];
    lv_obj_t * sel_btn;
    lv_obj_t * test_btn;
    lv_obj_t * cont_camera;
    lv_img_dsc_t img_camera;
    lv_task_t *camera_preview_task;
    lv_task_t *emmc_task;
    lv_task_t *cpu_overload_task;
} lv_engineer_hwtest_obj_ext_t;

enum
{
    AUTOTEST_PRESS_KEY = 0,
    AUTOTEST_VERSION_INFO,
    AUTOTEST_LCD_TOUCH,
    AUTOTEST_LCD_COLOR,
    AUTOTEST_CAMERA,
    AUTOTEST_SPEAKER,
    AUTOTEST_MICPHONE,
    AUTOTEST_CALL,
    AUTOTEST_MEMORY,
    AUTOTEST_BACKLIGHT,
    AUTOTEST_GRAVITY,
    AUTOTEST_BATTERY,
    AUTOTEST_VIBRATION,
    AUTOTEST_WIFI,
    AUTOTEST_GPS,
    AUTOTEST_SIMCARD,
    AUTOTEST_MAX = 0xFF
};
typedef uint8_t autotest_module_t;

enum
{
    AGINGTEST_SPEAKER = 0,
    AGINGTEST_LCD,
    AGINGTEST_CAMERA,
    AGINGTEST_CALL,
};
typedef uint8_t agingtest_module_t;

typedef void (*autotest_item_create) (struct _lv_obj_t * par);

/**********************
 * GLOBAL PROTOTYPES
 **********************/
lv_obj_t * factorymode_main_create(lv_obj_t * activity_obj);
lv_obj_t * factorymode_autotest_create(lv_obj_t * activity_obj);
lv_obj_t * factorymode_testreport_create(lv_obj_t * activity_obj);
lv_obj_t * factorymode_agingtest_create(lv_obj_t * activity_obj);
lv_obj_t * factorymode_reset_create(lv_obj_t * activity_obj);
lv_obj_t * factorymode_gcf_create(lv_obj_t * activity_obj);
lv_obj_t * factorymode_hwtest_create(lv_obj_t * activity_obj);

void factorymode_init_test_res(void);
void factorymode_save_test_res(void);
void factorymode_clear_test_res(void);
void factorymode_clear_agingtest_res(void);
lv_factory_obj_ext_t * factorymode_get_ext(void);
void autotest_test_item_create(lv_obj_t * par, uint8_t num);
watchLangTxtId_t autotest_get_item_textId(uint8_t num);
#if USE_CRANE_WATCH_KEYPAD
void autotest_key_event(struct keypad_param *arg);
#endif

void autotest_presskey_create(lv_obj_t * par);
void autotest_versioninfo_create(lv_obj_t * par);
void autotest_lcdarea_create(lv_obj_t * par);
void autotest_lcdcolor_create(lv_obj_t * par);
void autotest_lcdcolor_aging_create(lv_obj_t * par);
void autotest_lcdtest_result_create(lv_obj_t * par);
void autotest_camera_create(lv_obj_t * par);
void autotest_wifi_create(lv_obj_t * par);
void autotest_gps_create(lv_obj_t * par);
void autotest_speaker_create(lv_obj_t * par);
void autotest_micphone_create(lv_obj_t * par);
void autotest_simcard_create(lv_obj_t * par);
void autotest_call_create(lv_obj_t * par);
void autotest_memory_create(lv_obj_t * par);
void autotest_backlight_create(lv_obj_t * par);
void autotest_gravity_create(lv_obj_t * par);
void autotest_battery_create(lv_obj_t * par);
void autotest_vibration_create(lv_obj_t * par);
void autotest_connectivity_create(lv_obj_t * par);
void autotest_hal_stop(void);

void testreport_update(uint8_t num);
void agingtest_update(uint8_t num);

uint8_t autotest_aging_lcdtest_get_brightness(void);

/*for camera driver test begin*/
void camera_preview_test_start(void);
void camera_preview_test_stop(void);
/*for camera driver test end*/

#if USE_LV_WATCH_ENGINEER_MODE != 0
bool keep_screen_on_get_status(void);
#endif

/**********************
 *      MACROS
 **********************/
#define NV_FACTORYMODE_LEN          sizeof(nv_watch_factorymode_t)
#define NV_FACTORYMODE_OFFSET       0


#endif /*USE_LV_WATCH_FACTORY_MODE*/

#ifdef __cplusplus
} /* extern "C" */
#endif

#endif /*FACTORY_MODE_H*/
